var express = require('express'),
    // swig = require('swig-templates'),
    // request = require('request'),
    wx = require('../base/wx'),
    router = express.Router();

function getData(req, res, next) {
    var result = {
        "code": 200,
        "kind": "cup",
        "title": "iPhone6/6s系列",
        "message": {
            "diy": [{
                "product_id": 1,
                "name": "ip6/6s-光面",
                "url": "/uploads/product/20170724/5f67959b53c11d02bdbb7a81519187b9.jpg",
                "node_url": "/uploads/product/20170724/5f67959b53c11d02bdbb7a81519187b9.jpg",
                "type": "diy",
                "remark": "手机壳DIY"
            }, {
                "product_id": 42,
                "name": "ip6/6s-磨砂",
                "url": "/uploads/product/20170821/f2d8cc380e22573011abd3fee37ee09e.png",
                "node_url": "/uploads/product/20170821/f2d8cc380e22573011abd3fee37ee09e.png",
                "type": "diy",
                "remark": "手机壳DIY"
            }],
            "fix": [{
                "product_id": 17,
                "name": "i6/6s飞舞",
                "url": "/uploads/product/20170710/34a1e8a0ef9b91715a188266acfb3837.png",
                "node_url": "/uploads/product/20170710/34a1e8a0ef9b91715a188266acfb3837node.png",
                "type": "fix",
                "remark": "时尚手机壳"
            }, {
                "product_id": 23,
                "name": "i6/6火烈鸟",
                "url": "/uploads/product/20170802/73a3fdb3365170b3bfbcee77f06b270f.jpg",
                "node_url": "/uploads/product/20170802/73a3fdb3365170b3bfbcee77f06b270fnode.jpg",
                "type": "fix",
                "remark": "时尚手机壳"
            }, {
                "product_id": 45,
                "name": "iphone6/6s不换壳",
                "url": "/uploads/product/20171116/36dbbfcb9a19b0e1177a920bfd930669.png",
                "node_url": "/uploads/product/20171116/36dbbfcb9a19b0e1177a920bfd930669node.png",
                "type": "fix",
                "remark": "时尚手机壳"
            }, {
                "product_id": 46,
                "name": "iphone6/6s love music",
                "url": "/uploads/product/20171116/5a2862e30d1460b8aef0b24f488c6954.png",
                "node_url": "/uploads/product/20171116/5a2862e30d1460b8aef0b24f488c6954node.png",
                "type": "fix",
                "remark": "时尚手机壳"
            }]
        },
        "imsg": "返回数据成功"
    };

    var typeTitleObject = {
        diy: '手机壳DIY',
        fix: '时尚手机壳'
    };

    var message = [];
    for (let key in result.message) {
        message.push({
            type: key,
            type_cn: typeTitleObject[key],
            list: result.message[key]
        });
    }

    if (result.code == 200) {
        res.render('page/product', {
            title: result.title,
            stupid_data: message
        });
    }

    return;


    kakarotto.request.get({
        url: kakarotto.config.api.origin +'/index/index/modelProducts?user_id='+ (req.cookies.user_id?req.cookies.user_id:'') +'&nav_id='+ req.query.id
    }, function (error, response, body) {
        res.status(200).send('<h1>hello world</h1>');
        return;
        try {
            var result = JSON.parse(body);
            if (result.code == 200) {
                var rundData = {
                    title: result.title,
                    message: result.message
                };
                res.render('page/products-list', rundData);
            }
        } catch (e) {
            var err = new Error('Not Found');
            err.status = 500;
            next(err);
            return;
        }
    });
}

router.get('/', function(req, res, next) {
    wx.goAccessToken(req, res, function () {
        getData(req, res, next);
    });
});

module.exports = router;
